Ext.define('Admin.view.needWater.supplySitAnalysis.SupplySitAnalysisController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.supplySitAnalysisController',
    views: ['Admin.view.needWater.supplySitAnalysis.SupplySitAnalysis'],
    init: function () {
        var me = this;
        // Ext.getCmp('gsxsfxBtn1').fireEvent('click');
    },
    changeType1: function () {
        this.chartType = '1';
        this.initChart();
    },
    changeType2: function () {
        this.chartType = '2';
        this.initChart();
    },
    initChart: function (dom) {
        var me = this;
        var domid;
        if (this.chartId) {
            domid = this.chartId;
        } else {
            if (typeof dom.el === 'object') {
                domid = dom.el.id + '-body';
                this.chartId = domid;
            } else {
                return;
            }
        }
        var river = config.RIVER;
        var datas = this.getRandomData();
        var chartNum = datas.length;
        $('#' + domid).html('');
        var html = $('#' + domid);
        var row;
        var colNum = 4;
        var rowNum = -1;
        var h = 100 / Math.ceil(chartNum / 3);
        for (var i = 0; i < chartNum; i++) {
            if (i === 0 || i % 3 === 0) {
                row = $('<div class="row-chart" style="height: ' + h + '%;"></div>');
                html.append(row);
                rowNum++;
            }
            var tempNum = Math.floor((chartNum - rowNum * 3) / 3);
            if (tempNum === 0) {
                colNum = 12 / (chartNum - rowNum * 3);
            } else if (tempNum > 0) {
                colNum = 4;
            }
            row.append($('<div style="height: 100%;" id="supplySitAnalysis-chart-' + (i + 1) + '"></div>')
                .css({'flex': (colNum / 12) + '%'}));
        }

        $.each(datas, function (index, item) {
            var id = 'supplySitAnalysis-chart-' + (index + 1);
            me.buildChart(id, item.title, item.data);
        });
    },
    randomNum: function () {
        return parseFloat((Math.random() * 100).toFixed(2));
    },
    getRandomData: function () {
        var me = this;
        var type = this.chartType || '1';
        var river = config.RIVER;
        var areas1 = [];
        var areas2 = [];
        if (type == '1') {
            areas1 = ['临川', '南城', '黎川', '南丰', '崇仁', '乐安', '宜黄', '金溪', '资溪', '东乡', '广昌'];
            areas2 = ['宜春市', '萍乡市', '新余市'];
        } else if (type == '2') {
            areas1 = ['临水', '抚河中游廖坊以上', '抚河中游', '抚河中游廖坊以下', '盱江', '廖坊以上', '廖坊以下'];
            areas2 = ['芦溪县', '袁州区', '高安市', '上高县', '樟树市', '分宜县', '渝水区'];
        }
        var areas = [];
        if ('02' === river) {
            areas = areas1;
        } else if ('03' === river) {
            areas = areas2;
        }
        var data = [];
        $.each(areas, function (index, val) {
            data.push({
                title: val,
                data: [{
                    name: '生态',
                    data: [me.randomNum(), me.randomNum(), me.randomNum()]
                }, {
                    name: '生活',
                    data: [me.randomNum(), me.randomNum(), me.randomNum()]
                }, {
                    name: '工业',
                    data: [me.randomNum(), me.randomNum(), me.randomNum()]
                }, {
                    name: '农业',
                    data: [me.randomNum(), me.randomNum(), me.randomNum()]
                }]
            });
        });
        return data;
    },
    buildChart: function (domId, title, data) {
        $('#' + domId).highcharts({
            chart: {
                type: 'column'
            },
            title: {
                text: title
            },
            xAxis: {
                categories: ['需水', '计划', '实际']
            },
            yAxis: {
                min: 0,
                title: {
                    text: ''
                },
                stackLabels: {
                    enabled: true,
                    style: {
                        fontWeight: 'bold',
                        color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
                    }
                }
            },
            legend: {
                layout: 'horizontal',
                align: 'center',
                verticalAlign: 'bottom',
                borderWidth: 0
            },
            tooltip: {
                formatter: function () {
                    return '<b>' + this.x + '</b><br/>' +
                        this.series.name + ': ' + this.y + '<br/>' +
                        '总量: ' + this.point.stackTotal;
                }
            },
            plotOptions: {
                column: {
                    stacking: 'normal',
                    dataLabels: {
                        enabled: true,
                        color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
                        style: {
                            textShadow: '0 0 3px black'
                        }
                    }
                }
            },
            series: data,
            credits: {
                enabled: false
            }
        });
    }
})
